package com.hiray.demos.android_features

import android.app.Activity
import android.app.KeyguardManager
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import com.hiray.demos.R

class ScreenWallPaperActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_screen_wall_paper)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_DOWN ->
                setPressed(true, event.x, event.y)
            MotionEvent.ACTION_UP ->
                checkForTap(event.x, event.y)
        }
        return super.onTouchEvent(event)
    }

    var pressedX: Float = 0f
    var pressedY: Float = 0f
    var pressed: Boolean = false

    private fun setPressed(b: Boolean, x: Float, y: Float) {
        pressed = b
        pressedX = x
        pressedY = y
    }

    private fun checkForTap(x: Float, y: Float) {
        if (x == pressedX && y == pressedY)
            showUnLock()
    }

    private fun showUnLock() {
        val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        intent = keyguardManager.createConfirmDeviceCredentialIntent("解锁", "解锁")
        startActivityForResult(intent, 22)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == 22)
            finish()
    }
}
